package com.als.healthymananger.controller.impl;

import com.als.healthymananger.controller.AppointmentsApi;
import com.als.healthymananger.dto.AppointmentDto;
import com.als.healthymananger.entity.Appointment;
import com.als.healthymananger.service.AppointmentsService;
import com.als.healthymananger.utils.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/appointment")
public class AppointmentsController implements AppointmentsApi {
    @Autowired
    private AppointmentsService appointmentsService;

    @Override
    public RespBean createAppointmentDto(AppointmentDto appointment) {
       return appointmentsService.createAppointment(appointment)? RespBean.ok("申请预约成功，等待电话联系"): RespBean.badRequest("申请预约失败");
    }

    @Override
    public RespBean deleteAppointmentDto(int appointmentId) {
        return appointmentsService.removeById(appointmentId)? RespBean.ok("删除成功"): RespBean.badRequest("删除失败");
    }

    @Override
    public RespBean updateAppointmentDto(int appointmentId, Appointment appointment) {
        return appointmentsService.updateById(appointment)? RespBean.ok("修改成功"): RespBean.badRequest("修改失败");
    }

    @Override
    public RespBean getAppointmentDto(int appointmentId) {
        AppointmentDto dto = appointmentsService.getAppointmentDto(appointmentId);
        return dto == null? RespBean.badRequest("获取失败"): RespBean.ok("获取成功", dto);
    }

    @Override
    public RespBean getAppointmentDtoByUsername(String username) {
        log.info("username: {}", username);
        List<String> ids = appointmentsService.getAppointmentDtoByUserName(username);

        if (ids == null)
            return RespBean.badRequest("不存在预约");

//        List<String> list = ids.stream().map(id -> "/appointment/" + id).collect(Collectors.toList());

        List<AppointmentDto> list = ids.stream().map(id -> appointmentsService.getAppointmentDto(Integer.valueOf(id))).collect(Collectors.toList());

        return RespBean.ok("获取成功", list);
    }

    @Override
    public RespBean getAppointmentDtos() {
        List<Appointment> list = appointmentsService.list();
        return RespBean.ok("", list);
    }
}
